package com.supply.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.supply.pojo.ResultBean;
import com.supply.pojo.Role;
import com.supply.req.RoleGroup;
import com.supply.req.RoleReq;
import com.supply.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public ResultBean list(RoleReq roleReq){
        PageHelper.startPage(roleReq.getPageNum(),roleReq.getPageSize());
        List<Role> roles = roleService.queryByCondition(roleReq);
        return ResultBean.success(new PageInfo(roles));
    }

    @GetMapping("/queryAll")
    public ResultBean queryAll(){
        return ResultBean.success(roleService.queryByCondition(null));
    }

    @PostMapping("/saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody Role role){
        System.out.println("新增或修改的数据：" + role);
        int rs = roleService.saveOrUpdate(role);
        return rs >= 1 ? ResultBean.success() : ResultBean.error("角色名称已存在或服务器异常");
    }

    //设置角色所拥有的权限
    @PostMapping("/authorization")
    public ResultBean authorization(@RequestBody RoleGroup roleGroup){
        System.out.println("授权数据：" + roleGroup);
        boolean rs = roleService.authorizationByRoleId(roleGroup);
        return rs ? ResultBean.success() : ResultBean.error("角色名称已存在或服务器异常");
    }

    @DeleteMapping("/delete/{id}")
    public ResultBean delete(@PathVariable("id") Long id){
        System.out.println("要删除的部门id：" + id);
        int rs = roleService.deleteById(id);
        return rs >= 1 ? ResultBean.success() : ResultBean.error("该角色存在对应员工或服务器异常，删除失败");
    }
}
